// Written in the D Programming Language
/*
 * Copyright (c) 2008 OpenGLUtils
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * * Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * * Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 *
 * * Neither the name of 'OpenGLUtils' nor the names of its contributors 
 *   may be used to endorse or promote products derived from this software 
 *   without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
module openglutils.GLProgram;

import openglutils.GL;
import openglutils.GLObject;
import openglutils.GLVersion;
import openglutils.GLShader;

/**
 * GLProgram is the GLSL shader program object. It provides an interface for calling those functions pertaining to
 * programs in OpenGL.
 */
public class GLProgram : GLObject
{
	/**
	 * Constructs a new GLProgram.
	 * 
	 * Throws: GLException if the OpenGL driver doesn't support version 2.0 or higher
	 */
	public this()
	{
		super();
		
    	if (getGLVersion() < GLVersion.Version20)
		{
    		throw new GLException("OpenGL version 2.0 is required.");		
		}
	}
	
	private void _attachShader(GLuint T)(GLShader!(T) shader)
	{
		glAttachShader(getId(), shader.getId());
	}

	/**
	 * Attach a vertex shader.
	 * 
	 * Params:
	 * 	shader = the shader to be added
	 */
	public alias _attachShader!(GL_VERTEX_SHADER) attachShader;
	
	/**
	 * Attach a fragment shader.
	 * 
	 * Params:
	 * 	shader = the shader to be added
	 */
	public alias _attachShader!(GL_FRAGMENT_SHADER) attachShader;

	private void _detachShader(GLuint T)(GLShader!(T) shader)
	{
		glDetachShader(getId(), shader.getId());
	}

	/**
	 * Detach a vertex shader.
	 * 
	 * Params:
	 * 	shader = the shader to be added
	 */
	public alias _detachShader!(GL_VERTEX_SHADER) detachShader;
	
	/**
	 * Detach a fragment shader.
	 * 
	 * Params:
	 * 	shader = the shader to be added
	 */
	public alias _detachShader!(GL_FRAGMENT_SHADER) detachShader;

	/**
	 * Links this program.
	 */
	public void link()
	{
		glLinkProgram(getId());
	}
	
	/**
	 * Uses this program.
	 */
	public void use()
	{
		glUseProgram(getId());
	}

	/**
	 * Gets the value for the given parameter.
	 * 
	 * Params:
	 * 	pname = the program parameter name
	 * 
	 * Returns: The parameter's value.
	 */
	public GLint getParameter(GLenum pname)
	{
		GLint param;
		glGetProgramiv(getId(), pname, &param);
		return param;
	}
	
	/**
	 * Gets the length of the current info log for this program.
	 * 
	 * Returns: The info log length
	 */
	public GLint getInfoLogLength()
	{
		return getParameter(GL_INFO_LOG_LENGTH);
	}
	
	/**
	 * Gets the info log for this program.
	 * 
	 * Returns: The info log if there is something, an empty string otherwise.
	 */
	public char[] getInfoLog()
	{
		GLint length;
		char[] log = new char[getInfoLogLength()];
		glGetProgramInfoLog(getId(), getInfoLogLength(), &length, log.ptr);
		return length > 0 ? log : "";
	}
	
	/**
	 * Validates the program. This is mainly used while developing the application. Consider putting all your calls
	 * to this function inside a debug-block. Also when the validation fail remember to check the info log.
	 *  
	 * See_Also: getInfoLog
	 */
	public bool validate()
	{
		glValidateProgram(getId());
		return getParameter(GL_VALIDATE_STATUS) == GL_TRUE;
	}

	/**
	 * Destroys the given OpenGL program.
	 */
	public override void destroy()
    {
		glDeleteProgram(getId());
    }

	/**
	 * Generates a new OpenGL program id.
	 * 
	 * Returns: The generated id.
	 */
	protected override GLuint generateID()
	{
		return glCreateProgram();
	}    

	/**
	 * Checks if the given parameter is a valid program.
	 * 
	 * Returns: True if the parameter is a program, false otherwise.
	 */
	public static bool isProgram(GLuint program)
	{
		return glIsProgram(program) == GL_TRUE;
	}
}